package com.gouaixin.bee.beeframework2.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.gouaixin.bee.beeframework2.R;
import com.gouaixin.bee.beeframework2.bean.USERINFO;
import com.gouaixin.bee.beeframework2.tool.mLog;

import java.util.List;

import cn.bmob.im.BmobChatManager;
import cn.bmob.im.bean.BmobChatUser;
import cn.bmob.im.bean.BmobMsg;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

/**
 * Created by Administrator on 2015/3/20.
 */
//消息发送测试界面
public class MessageTestActivity extends Activity {
    Context context = MessageTestActivity.this;
    EditText target;
    EditText content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.message_test);


        target = (EditText) findViewById(R.id.target);
        content = (EditText) findViewById(R.id.content);
        Button send = (Button) findViewById(R.id.send);

        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String targetStr = target.getText().toString();
                String contentStr = content.getText().toString();
                sendMessage(targetStr, contentStr);
            }
        });


    }

    BmobChatManager mBmobChatManager = BmobChatManager.getInstance(context);
    BmobChatUser targetUser;
    USERINFO userinfo;
    String targetId;
    String msg = "聊天消息测试";

    private void sendMessage(String targetStr, String contentStr) {

        mLog.e("查询开始：");

        BmobQuery<USERINFO> query01 = new BmobQuery<USERINFO>();
        query01.addWhereEqualTo("jid", targetStr);
        query01.findObjects(context, new FindListener<USERINFO>() {
            @Override
            public void onSuccess(List<USERINFO> object) {
                mLog.e("查询成功：共" + object.size() + "条数据。" + object.toString());
                userinfo = object.get(0);
                //
////                // 组装BmobMessage对象
                BmobMsg message = BmobMsg.createTextSendMsg(context, targetId, msg);
//                        // 默认发送完成，将数据保存到本地消息表和最近会话表中
                mBmobChatManager.sendTextMessage(targetUser, message);

            }

            @Override
            public void onError(int code, String msg) {
                mLog.e("查询失败：" + msg);
            }
        });

        mLog.e("查询结束：");


        mLog.e("查询开始：");

        BmobQuery<BmobChatUser> query = new BmobQuery<BmobChatUser>();
        query.addWhereEqualTo("username", "03297820");
        query.findObjects(context, new FindListener<BmobChatUser>() {
            @Override
            public void onSuccess(List<BmobChatUser> object) {
                mLog.e("查询成功：共" + object.size() + "条数据。" + object.toString());
                targetUser = object.get(0);
                targetId = targetUser.getObjectId();

                //
////                // 组装BmobMessage对象
                BmobMsg message = BmobMsg.createTextSendMsg(context, targetId, msg);
//                        // 默认发送完成，将数据保存到本地消息表和最近会话表中
                mBmobChatManager.sendTextMessage(targetUser, message);

            }

            @Override
            public void onError(int code, String msg) {
                mLog.e("查询失败：" + msg);
            }
        });

        mLog.e("查询结束：");


    }


//    Object obj="";String key="";String value="";
    //        queryKeyInObject(obj,key,value);
}
